﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Biz;
using Entity;
using Util;

namespace TaskMgr
{
    
    public partial class frmEditTask : Form
    {
        public frmEditTask()
        {
            InitializeComponent();
        }
        // 标记页面的状态是编辑还是新建；false新建 | true编辑
        public bool IsEditMode = false;
        // 只有编辑状态会有值
        public int TaskId;
        // 编辑/添加成功，关闭页面回调主页面的刷新；
        public event RefreshList RefreshTaskList;

        private void btnOK_Click(object sender, EventArgs e)
        {
            Guard.Info("frmEditTask.btnOK_Click:Start");
            try
            {
                BizTask bizTask = new BizTask();
                bizTask.TaskName = this.txtTaskName.Text;
                bizTask.ProjId = int.Parse(this.cbProj.SelectedValue.ToString());
                bizTask.ProjName = this.cbProj.Text;
                bizTask.TaskDescription = this.txtTaskComment.Text;
                bizTask.PlanFinishDate = this.dtPlanCompleteDate.Value;
                bizTask.Type = (TaskType)Enum.Parse(typeof(TaskType), this.cmbTaskType.Text);
                List<int> empIDs = new List<int>();
                // 获取任务委派人列表
                foreach (ComboBox cmb in this.AssignerIDs)
                {
                    empIDs.Add((int)cmb.SelectedValue);
                }
                // 获取任务列表
                List<string> stepNames = new List<string>();
                foreach (TextBox txt in this.ActivityNames)
                {
                    stepNames.Add(txt.Text);
                }
                
                bizTask.Id = TaskId;
                TaskService service = new TaskService(bizTask);
                if (!IsEditMode)
                    service.CreateWFAndNodes(stepNames, empIDs);
                else
                    service.UpdateWFAndNodes(stepNames, empIDs);
                // 刷新主页面
                this.RefreshTaskList();
                this.Close();
            }
            catch (Exception ex)
            {
                Guard.TraceException(ex, "");
            }

            Guard.Info("frmEditTask.btnOK_Click:End");
        }

        private void frmEditTask_Load(object sender, EventArgs e)
        {
            try
            {
                this.cmbAssigner.DataSource = EmployeeService.GetAllEmployeeList();
                this.cmbTaskType.DataSource = DictionService.GetTaskTypes();

                this.AssignerIDs.Add(this.cmbAssigner);
                this.ActivityNames.Add(this.txtStepName);

                // 填充项目列表
                this.cbProj.DisplayMember = "ProjectName";
                this.cbProj.ValueMember = "Id";
                this.cbProj.DataSource = ProjectService.GetProjects();

                // 如果是编辑模式，根据taskId进行填充编辑框
                if (this.IsEditMode)
                {
                    this.FillTaskContext(this.TaskId);
                    this.btnContinue.Visible = false;
                }
            }
            catch (Exception ex)
            {
                Guard.TraceException(ex, "");
            }
        }

        private List<ComboBox> AssignerIDs = new List<ComboBox>();
        private List<TextBox> ActivityNames = new List<TextBox>();
        private int activityCount = 1;
        private const int PIX = 30;

        private void btnAddStep_Click(object sender, EventArgs e)
        {
            try
            {
                AddControlForStep();
            }
            catch (Exception ex)
            {

            }
        }

        private void AddControlForStep()
        {
            AddControlForStep("", 0);
        }

        /// <summary>
        /// 每增加一个step，就需要在页面中增加4个控件
        /// </summary>
        /// <param name="pTaskName">需要填充的任务名称</param>
        /// <param name="pEmpId">为步骤指定的员工ID</param>
        private void AddControlForStep(String pTaskName, int pEmpId)
        {
            Label lblStepNameForCreate = new Label();
            lblStepNameForCreate.Text = lblStepName.Text;
            lblStepNameForCreate.Size = lblStepName.Size;
            Point p = new Point(lblStepName.Location.X, lblStepName.Location.Y + PIX * activityCount);
            lblStepNameForCreate.Location = p;
            this.Controls.Add(lblStepNameForCreate);

            TextBox txtStepNameForCreate = new TextBox();
            txtStepNameForCreate.Size = this.txtStepName.Size;
            Point p2 = new Point(p.X + 70, p.Y);
            txtStepNameForCreate.Location = p2;
            txtStepNameForCreate.Text = pTaskName;
            this.Controls.Add(txtStepNameForCreate);
            this.ActivityNames.Add(txtStepNameForCreate);

            Label lblAssignerForCreate = new Label();
            lblAssignerForCreate.Text = this.lblAssigner.Text;
            lblAssignerForCreate.Size = this.lblAssigner.Size;
            Point p3 = new Point(p.X + 345, p.Y + 3);
            lblAssignerForCreate.Location = p3;
            this.Controls.Add(lblAssignerForCreate);

            ComboBox cmbAssignerForCreate = new ComboBox();
            cmbAssignerForCreate.Size = this.cmbAssigner.Size;
            cmbAssignerForCreate.DataBindings.Add(new System.Windows.Forms.Binding("SelectedValue", this.bizEmployeeListBindingSource, "Id", true));
            cmbAssignerForCreate.DataSource = this.bizEmployeeListBindingSource;
            cmbAssignerForCreate.DisplayMember = "EmpName";
            cmbAssignerForCreate.FormattingEnabled = true;
            cmbAssignerForCreate.ValueMember = "Id";
            cmbAssignerForCreate.DataSource = EmployeeService.GetAllEmployeeList();
            cmbAssignerForCreate.SelectedValue = pEmpId;

            Point p4 = new Point(p3.X + 45, p3.Y);
            cmbAssignerForCreate.Location = p4;
            

            this.Controls.Add(cmbAssignerForCreate);
            cmbAssignerForCreate.SelectedValue = pEmpId;
            this.AssignerIDs.Add(cmbAssignerForCreate);

            this.btnOK.Location = new Point(this.btnOK.Location.X, this.btnOK.Location.Y + PIX);
            this.btnCancel.Location = new Point(this.btnCancel.Location.X, this.btnCancel.Location.Y + PIX);
            this.btnContinue.Location = new Point(this.btnContinue.Location.X, this.btnContinue.Location.Y + PIX);

            this.Size = new Size(this.Size.Width, this.Size.Height + PIX);
            this.activityCount++;
        }

        private void btnContinue_Click(object sender, EventArgs e)
        {

        }

        private void FillTaskContext(int pTaskId)
        {
            BizTask task = TaskService.GetTaskByID(pTaskId);

            this.txtTaskName.Text = task.TaskName;
            this.txtTaskComment.Text = task.TaskDescription;

            this.cmbTaskType.Text = task.Type.ToString();
            this.dtPlanCompleteDate.Value = task.PlanFinishDate;
            this.lblRealFinishDate.Text = task.RealFinishDate.ToString("yyyy-MM-dd HH:mm:ss");

            this.cbProj.SelectedValue = task.ProjId;

            bool isFirst = true;
            foreach (BizWorkflowSequeuece step in task.TaskWF.AllNodes)
            {
                // 如果是首次，则在已有的控件中填充
                if (isFirst)
                {
                    this.txtStepName.Text = step.StepName;
                    this.cmbAssigner.SelectedValue = step.EmpID;
                    isFirst = false;
                    continue;
                }
                // 如果不是首次，需要创建控件，并填充
                this.AddControlForStep(step.StepName, step.EmpID);
            }
        }

        /// <summary>
        /// 取消按钮，关闭窗体
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
